﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace KeyGen
{
    public partial class frmKeyGenMain : Form
    {
        private string keyCpu = "Win32_Processor";
        private string keyHardDisk = "Win32_DiskDrive";
        private long size = 0;
        private string raw1 = string.Empty;
        private string raw2 = string.Empty;
        private string random = "NhatMinh12bct45833VietNamhp663835";
        private string key = "NM@#891AbC&*";
        private MD5 md5Hash = null;

        public frmKeyGenMain()
        {
            InitializeComponent();
            this.ActiveControl = txtMaKichHoat;
            raw1 = this.GetInfoMain();
            raw2 = this.GetString(raw1);

        }

        private string GetInfoMain()
        {
            string result = "";
            string serialCpu = "";
            string serialHdd = "";
            try
            {
                ManagementObjectSearcher cpu = new ManagementObjectSearcher("select * from " + keyCpu);
                ManagementObjectSearcher hdd = new ManagementObjectSearcher("select * from " + keyHardDisk);

                ManagementObjectCollection mocCpu = cpu.Get();
                ManagementObjectCollection mocHdd = hdd.Get();

                foreach (ManagementObject mo in mocCpu)
                {
                    serialCpu = mo["ProcessorId"].ToString();
                    break;
                }

                foreach (ManagementObject mo in mocHdd)
                {
                    if (long.Parse(mo["Size"].ToString()) > size)
                    {
                        size = long.Parse(mo["Size"].ToString());
                        serialHdd = mo["SerialNumber"].ToString();
                    }
                }

                result = serialCpu + serialHdd;
            }
            catch (Exception)
            {
            }

            if (result.Length == 0)
            {
                result = random;
            }
            else if (result.Length < 10)
            {
                result = result + random;
            }

            return result;
        }

        private string GetString(string str)
        {
            string temp = string.Empty;
            foreach (char c in str)
            {
                if (char.IsLetterOrDigit(c))
                {
                    temp += c;
                }
            }

            return temp;
        }

        private string Convert(string str)
        {
            long temp1 = 0;
            long temp2 = 1;
            string output = string.Empty;
            int intTemp = 0;

            foreach (char c in str)
            {
                if (char.IsLetter(c))
                {
                    intTemp = ((int)(c - '0'));
                    temp1 += intTemp;
                }
                else if (char.IsNumber(c))
                {
                    if (((int)(c - '0')) != 0)
                    {
                        intTemp = ((int)(c - '0'));
                        temp2 *= intTemp;
                    }
                }
            }

            if ((temp1 + temp2).ToString().Count() > 10)
            {
                output = (temp1 + temp2).ToString().Substring(0, 10).Insert(5, "-");
            }
            else if ((temp1 + temp2).ToString().Count() > 5)
            {
                output = (temp1 + temp2).ToString().Insert(5, "-");
            }
            else
            {
                output = (temp1 + temp2).ToString();
            }

            return output;
        }

        private string Encrypt(string str, string key)
        {
            md5Hash = MD5.Create();
            return GetMd5Hash(md5Hash, str + key).Substring(0, 25).Insert(5, "-").Insert(11, "-").Insert(17, "-").Insert(23, "-").ToUpper();
        }

        static string GetMd5Hash(MD5 md5Hash, string input)
        {

            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }

        static bool VerifyMd5Hash(MD5 md5Hash, string input, string hash, string key)
        {
            string hashOfInput = GetMd5Hash(md5Hash, input + key).Substring(0, 25).Insert(5, "-").Insert(11, "-").Insert(17, "-").Insert(23, "-").ToUpper();
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void textBox2_MouseDown(object sender, MouseEventArgs e)
        {
            ((TextBox)sender).SelectAll();
        }

        private void btTaoMa_Click(object sender, EventArgs e)
        {
            //txtMaSanPham.Text = (this.Convert(raw2));
            if (txtMaSanPham.Text.Trim().Length > 0 && txtPass.Text.Trim().Equals("71PhamHuuDieu"))
            {
                txtMaKichHoat.Text = Encrypt(txtMaSanPham.Text, this.key);
            }
            else
            {
                MessageBox.Show(@"Bạn phải nhập mã sản phẩm và Password", @"Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
